package fun.yizhierha.tools.other.controller;

import fun.yizhierha.common.annotation.Log;
import fun.yizhierha.common.base.BaseErrDto;
import fun.yizhierha.common.exception.BizCodeEnum;
import fun.yizhierha.common.utils.ValidUtils;
import fun.yizhierha.common.utils.*;
import fun.yizhierha.tools.other.domain.ToolLocalStorage;
import fun.yizhierha.tools.other.domain.vo.CreateToolLocalStorageVo;
import fun.yizhierha.tools.other.domain.vo.UpdateToolLocalStorageVo;
import fun.yizhierha.tools.other.domain.vo.RetrieveToolLocalStorageVo;

import fun.yizhierha.tools.other.service.ToolLocalStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;

/** generated by EH-Admin
* @author erha
* @date Sun Jan 01 17:11:43 CST 2023
**/
@Api(tags = "系统管理:存储管理")
@RestController
@RequestMapping("/api/tools/storage")
@RequiredArgsConstructor
public class ToolLocalStorageController{

    private final ToolLocalStorageService toolLocalStorageService;

    @ApiOperation("获取存储")
    @Log("获取存储")
    @GetMapping
    @PreAuthorize("@eh.check('tools:storage:local:list')")
    public R<PageUtils<ToolLocalStorage>> list(RetrieveToolLocalStorageVo retrieveToolLocalStorageVo, Query.PageVo pageVo){
        PageUtils<ToolLocalStorage> res = toolLocalStorageService.list(retrieveToolLocalStorageVo,pageVo);
        return R.<PageUtils<ToolLocalStorage>>ok().setData(res);
    }

    @ApiOperation("新增存储")
    @Log("新增存储")
    @PostMapping
    @PreAuthorize("@eh.check('tools:storage:local:add')")
    public R<List<BaseErrDto>> add(@RequestParam("name")String name, @RequestParam("file")MultipartFile file){
        if (name == null) {
            return R.<List<BaseErrDto>>error(
                    BizCodeEnum.Client_Error_CRUD.getCode(), BizCodeEnum.Client_Error_CRUD.getMsg()
            );
        }
        CreateToolLocalStorageVo createToolLocalStorageVo = new CreateToolLocalStorageVo();
        createToolLocalStorageVo.setName(name);
        toolLocalStorageService.save(createToolLocalStorageVo,file);
        return R.ok();

    }

    @ApiOperation("修改存储")
    @Log("修改存储")
    @PutMapping
    @PreAuthorize("@eh.check('tools:storage:local:edit')")
    public R<List<BaseErrDto>> edit(@Validated @RequestBody ValidList<UpdateToolLocalStorageVo> updateToolLocalStorageList,
                    BindingResult bindingResult){
        List<BaseErrDto> errDtoList = ValidUtils.getBaseErrDtoByBindingRes(updateToolLocalStorageList, bindingResult);
        if (errDtoList.isEmpty()) {
            toolLocalStorageService.edit(updateToolLocalStorageList,errDtoList);
            if (errDtoList.isEmpty()){
                return R.ok();
            }else {
                return  R.<List<BaseErrDto>>error(
                        BizCodeEnum.Client_Error_CRUD.getCode(),
                        BizCodeEnum.Client_Error_CRUD.getMsg()
                ).setData(errDtoList);
            }
        }
        return  R.<List<BaseErrDto>>error(
                BizCodeEnum.Client_Error_CRUD.getCode(),
                BizCodeEnum.Client_Error_CRUD.getMsg()
        ).setData(errDtoList);
    }

    @ApiOperation("删除存储")
    @Log("删除存储")
    @DeleteMapping
    @PreAuthorize("@eh.check('tools:storage:local:del')")
    public R del(@RequestBody Set<Long> ids){
        toolLocalStorageService.remove(ids);
        return R.ok();
    }

    @ApiOperation("导出数据")
    @Log("导出数据")
    @GetMapping("/download")
    @PreAuthorize("@eh.check('tools:storage:local:list')")
    public void download(HttpServletResponse response){
        toolLocalStorageService.download(response);
    }

}